<?php
namespace app\api\controller;
use app\api\model\Log;
use app\api\model\ProductOrder;
use app\api\model\Shop;
use app\api\model\UserMoneyOrder;
use Payment\Common\PayException;
use think\Controller;
require_once VENDOR_PATH . 'riverslei/payment/autoload.php';
require_once VENDOR_PATH . 'riverslei/payment/examples/testNotify.php';
class Notify extends Controller
{

    public function aliNotify(){
        $request = $_REQUEST;
        if(isset($request['passback_params'])){
            $data = explode("_",$request['passback_params']);
            if(count($data)<2){
                (new Log())->dataUpdate(['content'=>json_encode($data),"type"=>3]);
                return false;
            }
            $source = $data[0];
            $order_type = $data[1];
            if($source==1){
                $aliConfig = require_once ALI_PATH . '/webaliconfig.php';
            } elseif ($source==2){
                $aliConfig = require_once ALI_PATH . '/aliconfig.php';
            }
        }
        else{
            (new Log())->dataUpdate(['content'=>json_encode($_REQUEST)]);
            return false;
        }
        (new Log())->dataUpdate(['content'=>json_encode($_REQUEST),'type'=>1]);
        $callback = new \TestNotify();
            $config = $aliConfig;
            $type = 'ali_charge';
        try {
            //$retData = Notify::getNotifyData($type, $config);// 获取第三方的原始数据，未进行签名检查

            $ret = \Payment\Client\Notify::run($type, $config, $callback);// 处理回调，内部进行了签名检查
           if($ret =="success"){ //签名成功
               if($order_type==1){ //1   表示总关联订单
                  (new ProductOrder())->OrderNotify($_POST,2,$source);
               }
               if($order_type==2){ //2   表示充值订单
                  (new UserMoneyOrder())->OrderNotify($_POST,2,$source);
               }
               if($order_type==3){//3   商家支付订单
                   (new Shop())->OrderNotify($_POST);
               }
               if($order_type==4){// 4 子订单 支付
                  (new ProductOrder())->OrderNotify($_POST,2,$source);
               }
           }else{
               (new Log())->dataUpdate(['content'=>json_encode($ret),'type'=>2]);
           }
        } catch (PayException $e) {
            echo $e->errorMessage();
            exit;
        }
    }
    //微信回调
    public function wxNotify(){
        $xml = file_get_contents('php://input');
        $result  = xmltoArray($xml);
        $result = objectToArray($result);
        $sign = MakeSign($result);
        if($sign!=$result['sign']){
            $log = new Log();
            $log->dataUpdate(['content'=>"签名错误"]);
            return false;
        }
        if($result['result_code']=="SUCCESS"){
            $result['trade_no'] =$result['transaction_id'];
            if(strpos($result['out_trade_no'],"PO")!==false){
                (new ProductOrder())->OrderNotify($result,1,1);
            }
            elseif(strpos($result['out_trade_no'],"UMO")!==false){ //2   表示充值订单
               (new UserMoneyOrder())->OrderNotify($result,1,1);
            }
            elseif(strpos($result['out_trade_no'],"S")!==false){//3   商家支付订单
                (new Shop())->OrderNotify($result);
            }
        }
    }
    //微信回调
    public function wxAppNotify(){
        $xml = file_get_contents('php://input');
        $result  = xmltoArray($xml);
        $request = objectToArray($result);
        (new Log())->dataUpdate(['content'=>json_encode($request),"type"=>99]);

        if(isset($request['attach'])){
            $data = explode("_",$request['attach']);
            if(count($data)<2){
//                (new Log())->dataUpdate(['content'=>json_encode($data),"type"=>3]);
                return false;
            }
            $source = $data[0];
            $order_type = $data[1];
            $wxConfig = require_once PAY_PATH .'wxappconfig.php';
        }
        else{
            return false;
        }
        $callback = new \TestNotify();
        $config = $wxConfig;
        $type = 'wx_charge';
        try {
            $ret = \Payment\Client\Notify::run($type, $config, $callback);// 处理回调，内部进行了签名检查
            $ret  = xmltoArray($ret);
            $ret = objectToArray($ret);//有返回码 如果是 return_code:success 和 return_msg:ok
            if($ret['return_code'] =="SUCCESS"){ //签名成功
                $request['trade_no'] =$request['transaction_id'];
                if($order_type==1){ //1   表示总关联订单
                    (new ProductOrder())->OrderNotify($request,1,$source);
                }
                if($order_type==2){ //2   表示充值订单
                    (new UserMoneyOrder())->OrderNotify($request,1,$source);
                }
                if($order_type==3){//3   商家支付订单
                    (new Shop())->OrderNotify($request);
                }
                if($order_type==4){// 4 子订单 支付
                    (new ProductOrder())->OrderNotify($request,1,$source);
                }
            }else{
                (new Log())->dataUpdate(['content'=>json_encode($ret),'type'=>2]);
            }
        } catch (PayException $e) {
            echo $e->errorMessage();
            exit;
        }
    }
}

